package learn.spring;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * 配置定时任务多线程
 * 
 * @author PinWei Wan
 * @since 2.6.9
 */
@EnableScheduling
public class EnableSchedulingConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadFactory threadFactory = new CustomizableThreadFactory("scheduled-pool-");
        // 设定一个长度10的定时任务线程池
        ExecutorService pool = new ScheduledThreadPoolExecutor(10, threadFactory);
        taskRegistrar.setScheduler(pool);
    }
}
